package io.plus.interactive.collections;

import io.plus.interactive.Io;
import io.plus.server.ServerConfiguration;

import java.util.Collection;
import java.util.concurrent.CopyOnWriteArraySet;


public class IpBlackList {
    public final CopyOnWriteArraySet ipSet = new CopyOnWriteArraySet();
    private ServerConfiguration serverConfiguration;

    public IpBlackList(ServerConfiguration serverConfiguration) {
        this.serverConfiguration = serverConfiguration;
    }

    public boolean add(String ip) {
        //先添加到黑名单列表
        //删除相关连接
        ipSet.add(ip);
        Io.remove(serverConfiguration, ip, "ip[" + ip + "]被加入了黑名单, " + serverConfiguration.getName());
        return true;
    }

    public void clear() {

        ipSet.clear();
    }

    public Collection<String> getAll() {
        return ipSet;
    }

    /**
     * 是否在黑名单中
     */
    public boolean isInBlacklist(String ip) {
        return ipSet.contains(ip);
    }

    /**
     * 从黑名单中删除
     */
    public void remove(String ip) {
        ipSet.remove(ip);
    }
}
